<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* 	Class Name	: Jurnal
	Author 		: Iswan Putera S.Kom
	Version		: 1.0
	Date		: 27 May 2013
*/

class Jurnal extends CI_Controller
{
	function __construct()
	{
		parent::__construct();	
		$this->load->model('main_model');
		$this->load->library('zetro_auth');
		//$this->load->library('zetro_func');
		//force_ssl();
	}
	
	function index()
	{
			$data=array();
			$data['id_unit']=$this->zetro_auth->menus('jurnal');
			$fld="<span class='add-on'>Unit Bisnis</span><select class='span2' id='id_unit' name='id_unit'>".
			$this->zetro_form->generate_dropdown_content('akun_unitbussiness','id_unit','nm_unit')."</select>";
			$this->zetro_form->AddUb($fld);
			$this->zetro_form->AddFilter($this->zetro_form->FilterBlnThn('Bulan','Tahun',true));
			$data['tab_header']=$this->zetro_form->generate_tab_header('List Jurnal',true);
			$this->zetro_form->frm_file('asset/frm/zetro_akun.frm');
			$this->zetro_auth->menus('jurnal');
			$content='';
			$this->zetro_form->section('jurnallist');
			$this->zetro_form->action_col(true);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->zetro_form->list_std('NewList','100%');
			//generate modul new jurnal
			$this->zetro_form->buttons(true,'Simpan');
			$this->zetro_form->section('newJurnal');
			//$this->zetro_form->AddFields("<input type='hidden' id='notrans' name='notrans' value=''/>");
			//$this->zetro_form->Modale('frm1');
			$data['ub']=$this->zetro_form->generate_dropdown_content('akun_unitbussiness','id_unit','nm_unit');
			$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm1'),'New Jurnal','AddNew');
			//generate edit form
			$this->zetro_form->buttons(false,'Simpan');
			$this->zetro_form->section();
			$data['modal2']=$this->zetro_form->dialog($this->form_edit_jurnal(),'Jurnal','edit',true);
			$this->zetro_form->buttons(true,'Tambah');
			$this->zetro_form->section("jurnal");
			$this->zetro_form->tp_ahead($this->get_akun());
			$this->zetro_form->AddFields("<input type='hidden' id='id_trans' name='id_trans' value=''/>");
			$this->zetro_form->Modale('frm3');
			$data['modal3']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm3'),'Add Jurnal Content','AddC');
			//print_r($data);
			//send data to view
			$this->load->view('admin/header');
			$this->load->view('accounting/akun_jurnal',$data);
			$this->load->view('admin/footer');
	}
	
	function get_nomor_jurnal()
	{
		$nom="";$urut="";
		$tgl=explode("/",$_POST['tgl']);

		$data=$this->main_model->call_sp('sp_jurnal_number',tglToSql($_POST['tgl']));
		foreach($data as $r)
		{
			$nom=$r->nom;
		}
		echo $nom ;
	}
	function set_jurnal_head()
	{
		$data=array();
		$data['tgljurnal']	=tglToSql($_POST['tgl_trans']);
		$data['notrans']	  =$_POST['id_jurnal'];
		$data['keterangan']   =empty($_POST['ket_trans'])?'':$_POST['ket_trans'];
		$data['created_by']   =$this->session->userdata('userid');
		$data['id_unit']	  =empty($_POST['id_unit'])?'1':$_POST['id_unit'];
		$this->main_model->prepared_data('jurnal_head',$data);
		echo success($this->main_model->set_data());
	}
	function set_jurnal_content()
	{
		$data=array();
		$akun=empty($_POST['id_akun'])?array("0"=>""):explode(":",$_POST['id_akun']);
		$data['idtrans']=empty($_POST['id_trans'])?'0':$_POST['id_trans'];
		$data['notrans']=empty($_POST['id_jurnal'])?'':$_POST['id_jurnal'];	
		$data['tgljurnal']=empty($_POST['tgl_trans'])?date('Y-m-d'):tglToSql($_POST['tgl_trans']);	
		$data['koderek']=$akun[0];	
		$data['nobukti']=empty($_POST['no_bukti'])?'':$_POST['no_bukti'];	
		$data['buktilain']=empty($_POST['bukti_lain'])?'':$_POST['bukti_lain'];	
		$data['reff']=empty($_POST['tp_trans'])?'0':$_POST['tp_trans'];
		($_POST['tp_trans']==0)?
			$data['debet']=empty($_POST['jml_trans'])?'0':str_replace(",","",$_POST['jml_trans']):
			$data['kredit']=empty($_POST['jml_trans'])?'0':str_replace(",","",$_POST['jml_trans']);
		$data['keterangan']=empty($_POST['ket_trans'])?'':$_POST['ket_trans'];	
		$data['costcenter']=empty($_POST['cc_trans'])?'':$_POST['cc_trans'];	
		$this->main_model->prepared_data('jurnal',$data);
		echo success($this->main_model->set_data());
	}
	function form_edit_jurnal()
	{
		$contents= "<div class='bs-docs-example1' style='font-size:small'>";
		$contents.="<form id='frm7' name='frm7' action='' method='post'>
					<table style='border-collapse:collapse' width='100%' class=''>
					<tr>
						<td width='10%' class='box_l box_b'>&nbsp;&nbsp;Tanggal</td>
						<td width='20%'><input type='text' id='tgl_trans' name='tgl_trans' value=''/></td>
						<td width='10%' class='box_l box_b'>No. Jurnal</td>
						<td width='20%'><input type='text' id='id_jurnal' name='id_jurnal' value=''/></td>
						<td width='10%' class='box_l box_b'>&nbsp;&nbsp;Debet</td>
						<td width='10%'><input type='text' id='t_debet' name='t_debet' value=''/></td>
					</tr>
					<tr>
						<td class='box_l box_b'>&nbsp;&nbsp;Keterangan</td>
						<td colspan='3'><input type='text' id='keterangan' name='keterangan' value='' class='w90'/></td>
						<td class='box_l box_b'>&nbsp;&nbsp;Kredit</td>
						<td><input type='text' id='t_kredit' name='t_kredit' value=''/></td>
					</tr>
					</table>";
	    /*$contents.="<form class='navbar-form pull-left'>
				    <span class='span1koma5'>Tanggal</span>
					<input type='text' id='tgl_trans' name='tgl_trans' class='span2'>
				    <span class='span1koma5'>Tanggal</span>
					<input type='text' id='tgl_trans' name='tgl_trans' class='span2'>
					</form>";*/
		$contents.= "</form></div>";
		$contents.= "<div class='bs-docs-example2' id='j_content' style='font-size:small;display:'>";
			$this->zetro_form->frm_file('asset/frm/zetro_akun.frm');
			$this->zetro_auth->menus('jurnal');
			$content='';
			$this->zetro_form->section('jurnallist');
			$this->zetro_form->action_col(true);
			$this->zetro_form->tabel_content($content);
			$contents.=$this->zetro_form->list_std('jurnal','100%');
		$contents.= "</div>";
		return $contents;
	}
	function get_detail_head()
	{
		$id_jurnal=$_POST['id'];
		$this->main_model->table_name('jurnal_head');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('notrans'=>$id_jurnal));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);	
	}
	function get_detail_content()
	{
		$notrans=$_POST['id'];
		$this->main_model->table_name('v_jurnal');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('idtrans'=>$notrans));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);	
	}
	function get_akun()
	{
		$data=array();$akuns="";
		$this->main_model->table_name('v_akun_display');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		//$akuns='[';
		foreach($data as $r)
		{
			$akuns.="&quot;".$r->kodeakun."&quot;,";
		}
		//$akuns.=']';
		return substr($akuns,0,-1);
	}
	function list_jurnal()
	{
		$data=array();$n=0;$datax=array();
		$content='';$i=0;$lst_usr=array();$ck='';
		$id_unit=empty($_POST['id_unit'])?'':$this->main_model->where(array('id_unit'=>$_POST['id_unit']));
		$this->main_model->where(array('bln'=>$_POST['bulan'],'thn'=>$_POST['tahun']));
		$ordby=empty($_POST['ordby'])?"no_jurnal desc":$_POST['ordby'];
		$data=$this->main_model->generate_list('v_jurnal_nomor',$ordby);
		$this->zetro_auth->menus('jurnal');
		foreach ($data as $r)
		{
				$i++;$ii=0;
				//get detail jurnal
				$ordby=empty($_POST['ordby'])?'no_jurnal':$_POST['ordby'];
				$this->main_model->where(array('no_jurnal'=>$r->no_jurnal));
				$datax=$this->main_model->generate_list('v_jurnal',$ordby);
				//======""----------------
				$alt=(($r->debet-$r->kredit)<0)?'error':'alert';
				$icon=($this->zetro_auth->cek_oto('e')!='' && $datax)?
					  $this->zetro_form->z_icon($r->no_jurnal.":".$r->jenis_trans,array('cus-add','cus-folder'),array('add','edited')):
					  $this->zetro_form->z_icon($r->no_jurnal.":".$r->jenis_trans,array('cus-add','cus-folder','cus-delete'),array('add','edited','hapus'));
				$content.=tr($alt.' tree-toggler\' id=\''.str_replace('.','_',$r->no_jurnal).'').td($i,false,'','','center').
						  td($r->tgl_trans,'',false,'','center').
						  td($r->no_jurnal,'',false,'','center').
						  td($r->jenis_trans,'',false,'','left\' nowrap=\'nowarp').
						  td(number_format($r->debet,2),'',false,'','right').
						  td(number_format($r->kredit,2),'',false,'','right').
						  td(number_format(($r->debet-$r->kredit),2),'',false,'','right').
						  td('','',false,'','center').
						  _tr();
				//content jurnal
				foreach($datax as $d)
				{
				$ii++;
				$iconz=($this->zetro_auth->cek_oto('e')!='')?
					  $this->zetro_form->z_icon($d->no_jurnal,array('cus-page-white-edit','cus-delete'),array('edit','deleted')):'';
					//  $this->zetro_form->z_icon($d->idtrans,array('cus-page-white-edit','cus-cross'),array('edit','del'));
				$content.=tr('tree '.str_replace('.','_',$r->no_jurnal).'\' id=\''.$d->no_jurnal.'\' style=\'display:none').td('','',false,'','center').
						  td($ii.".".nbs().$d->id_akun.". ".$d->cnama,'',false,'','left\' colspan=\'2').
						  td(nbs(5).$d->ket_trans,'',false,'','left\' nowrap=\'nowrap').
						  td(number_format($d->debet,2),'',false,'','right').
						  td(number_format($d->kredit,2),'',false,'','right').
						  td().
						  td('',false,'','','center').
						  _tr();
				}
				
		}
				$content.=$this->zetro_form->DataNotFound($data,8);
				echo $content;
	}
	function upd_jurnal_header()
	{
		$this->main_model->get_content(array('keterangan'=>addslashes($_POST['des'])));
		$this->main_model->where(array('notrans'=>$_POST['id']));
		$this->main_model->update_data('jurnal_head');
	}
	function del_jurnal_content()
	{
		$idtrans=$_POST['id'];
		$this->main_model->hapus_data('jurnal','idtrans',$idtrans);	
	}
	function del_jurnal_head()
	{
		$idtrans=$_POST['id'];
		$this->main_model->hapus_data('jurnal_head','notrans',$idtrans);	
	}
	function get_tahun_jurnal()
	{
		$data=array();
		$data=$this->main_model->generate_list('v_jurnal_tahun','tahun');
		$content="<option value=''>&nbsp</option>";
		foreach($data as $r)
		{
			$slct=($r->tahun==date('Y'))?" selected='selected'":"";
			$content.= "<option value='".$r->tahun."'".$slct.">".($r->tahun)."</option>";	
		}
		echo $content;
	}
	
	//tutup buku
	
	function jurnal_close()
	{
		$data['tab_header']=$this->zetro_form->generate_tab_header('Tutup Buku');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_akun.frm');
			$content='';
			$this->zetro_form->section('jurnalclose');
			$this->zetro_form->action_col(true);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->zetro_form->list_std('NewList','100%');
			
			$this->zetro_form->section('jurnalclose');
			$this->zetro_auth->menus('closing_periode');
			$this->zetro_form->addButtons(false);
			$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false,'Proses');
			$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std(),'Jurnal Closing Periode');
			
		$this->load->view('admin/header');
		$this->load->view('accounting/jurnal_close',$data);
		$this->load->view('admin/footer');
	}
	
	function set_period_close()
	{
		$this->main_model->get_content(array('status_buku'=>'Y','tglclose'=>tgltoSql($_POST['upto_tgl'])));
		$this->main_model->where(array('tgljurnal <='=>tgltoSql($_POST['upto_tgl']),'tgljurnal >='=>tgltoSql($_POST['from_tgl'])));
		echo (!$this->main_model->update_data('jurnal'))?
			"Periode Buku dari Tanggal :".$_POST['from_tgl']." s/d Tanggal : ".$_POST['upto_tgl']." telah di tutup":
			"Ada Kesalahan periode. Mohon di cek lagi";
	}
	
	function set_periode_open()
	{
		
	}
}

